package com.op.controller;


import com.op.entity.Menu;
import com.op.model.vo.ComponentRouterVo;
import com.op.model.vo.NavigationMenuTreeVo;
import com.op.model.vo.SelectMenuVo;
import com.op.response.ResponseResult;
import com.op.response.StateCode;
import com.op.service.MenuService;
import com.op.service.RoleMenuService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Menu)表控制层
 *
 * @author 孤焰
 * @since 2021-12-25 15:27:27
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;
    @Resource
    private RoleMenuService roleMenuService;

    /**
     * 查询所有数据
     * @param menu 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    public ResponseResult<List<Menu>> selectAll(Menu menu) {
        return ResponseResult.success(menuService.selectMenuList(menu));
    }

    /**
     * 查询菜单树列表
     * @param menu 查询条件
     * @return 菜单树列表
     */
    @Cacheable({"system:user:selectMenuTree"})
    @GetMapping("/selectMenuTree")
    public ResponseResult<List<Menu>> selectMenuTree(Menu menu) {
        return ResponseResult.success(menuService.selectMenuTree(menu));
    }

    /**
     * 查询菜单下拉树列表
     * @param menu 查询条件
     * @return 菜单下拉树列表
     */
    @Cacheable({"system:user:menuTreeLabel"})
    @GetMapping("/menuTreeLabel")
    public ResponseResult<List<SelectMenuVo>> menuTreeLabel(Menu menu) {
        return ResponseResult.success(menuService.menuTreeLabel(menu));
    }

    /**
     * 查询导航菜单树
     * @return 导航菜单树
     */
    @Cacheable({"system:user:navigationMenuTree"})
    @GetMapping("/navigationMenuTree")
    public ResponseResult<List<NavigationMenuTreeVo>> navigationMenuTree(Long roleId) {
        return ResponseResult.success(menuService.navigationMenuTree(roleId));
    }

    /**
     * 查询已被roleId绑定的菜单
     * @param roleId 角色id
     * @return 已被roleId绑定的菜单id
     */
    @GetMapping("/checkedMenuIdByRoleId")
    public ResponseResult<List<Long>> checkedMenuIdByRoleId(Long roleId) {
        return ResponseResult.success(roleMenuService.checkedMenuIdByRoleId(roleId));
    }

    /**
     * 查询所有的组件路由及绑定的角色
     * @return 组件路由Vo类
     */
    @Cacheable({"system:user:componentRouter"})
    @GetMapping("/componentRouter")
    public ResponseResult<List<ComponentRouterVo>> componentRouter() {
        return ResponseResult.success(menuService.componentRouter());
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{id}")
    public ResponseResult<Menu> selectOne(@PathVariable("id") Long id) {
        return ResponseResult.success(menuService.getById(id));
    }

    /**
     * 新增数据
     * @param menu 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public ResponseResult<Boolean> insert(Menu menu) {
        if (menuService.hasUniqueByMenuName(menu)) {
            return ResponseResult.fail(StateCode.NO_UNIQUE_MENU_NAME);
        }

        return ResponseResult.success(menuService.save(menu));
    }

    /**
     * 绑定菜单或修改菜单绑定关系
     * @param roleId 角色Id
     * @param menuIds 菜单Id列表
     * @return 操作结果
     */
    @PostMapping("/bindingMenus")
    public ResponseResult<Boolean> bindingMenus(@RequestParam("roleId") Long roleId,
                                                @RequestParam("menuIds") List<Long> menuIds) {
        return ResponseResult.success(roleMenuService.bindingMenus(roleId, menuIds));
    }

    /**
     * 修改数据
     * @param menu 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public ResponseResult<Boolean> update(Menu menu) {
        if (menuService.hasUniqueByMenuName(menu)) {
            return ResponseResult.fail(StateCode.NO_UNIQUE_MENU_NAME);
        } else if (menu.getId().equals(menu.getParentId())) {
            return ResponseResult.fail(StateCode.PARENT_MENU_EQUALS_ID);
        }

        return ResponseResult.success(menuService.updateById(menu));
    }

    /**
     * 修改或添加菜单
     * @param menu 实体对象
     * @return 操作结果
     */
    @PostMapping("/saveOrUpdate")
    public ResponseResult<Boolean> saveOrUpdate(Menu menu) {
        if (menuService.hasUniqueByMenuName(menu)) {
            return ResponseResult.fail(StateCode.NO_UNIQUE_MENU_NAME);
        } else if (menu.getId() != null && menu.getId().equals(menu.getParentId())) {
            return ResponseResult.fail(StateCode.PARENT_MENU_EQUALS_ID);
        }
        return ResponseResult.success(menuService.saveOrUpdate(menu));
    }

    /**
     * 删除菜单项
     * @param id 菜单项id
     * @return 删除结果
     */
    @DeleteMapping("/remove")
    public ResponseResult<Boolean> remove(Long id) {
        if (menuService.hasChildMenu(id)) {
            return ResponseResult.fail(StateCode.HAS_CHILD_MENU);
        } else if (menuService.hasBindingWithRole(id)) {
            return ResponseResult.fail(StateCode.HAS_BINDING_WITH_ROLE);
        }

        return ResponseResult.success(menuService.removeById(id));
    }
}

